home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / snap2PointsTo2Points.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  8.6 KB  |  256 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17. //
  18. //  Alias|Wavefront Script File
  19. //  MODIFY THIS AT YOUR OWN RISK
  20. //
  21. //  Creation Date:  July 17, 1998
  22. //  Author:        mgr 
  23. //
  24. //  Description:
  25. //      This is the action for snap 2 points to 2 points. It takes any 2 
  26. //        pair of point selections (curve point, surface point, edit point, poly
  27. //        vertex, etc.) and uses these as a vector. The first 2 points must
  28. //        be on the same object (i.e. it is the object to be transformed). The
  29. //        second pair of points define the position to translate to and the
  30. //        axis to rotate to.
  31. //
  32. //        Note: the 2 points do not define a scale, just an axis for rotation.
  33. //
  34. //  Input Arguments:
  35. //      None.
  36. //
  37. //  Return Value:
  38. //      None.
  39. //
  40.  
  41. proc string getObjectToMoveName( string $object )
  42. //
  43. //    Description:
  44. //        Returns the transform name of the object with no components in the 
  45. //        string e.g. curve1.u[0.5] will return curve1
  46. //
  47. {
  48.     string $name;
  49.  
  50.     // get the string before any "." or "->" - this could be a shape name
  51.     //
  52.     string $buffer[];
  53.     int $numTokens = `tokenize $object ".->" $buffer`;
  54.     if ( $numTokens >= 1 ) $name = $buffer[0];
  55.  
  56.     return $name;
  57. }
  58.  
  59. global proc snap2PointsTo2Points( int $moveParent, int $option )
  60. {
  61.     // $option = 1: left snap (pre 4.5 behaviour)
  62.     // $option = 2: middle snap
  63.     // $option = 3: right snap
  64.  
  65.     // $moveParent = 0: move the object defined by the first point
  66.     // $moveParent = 1: move the parent of the object
  67.     // $moveParent = 2: move the grandparent of the object
  68.     // $moveParent = 3: move the great-grandparent of the object
  69.  
  70.     // Get the list of nurbs curves/surfaces selected.
  71.     //
  72.     global int $gSelectCVsBit;
  73.     global int $gSelectMeshVerts;
  74.     global int $gSelectLatticePoints;
  75.     global int $gSelectParticles;
  76.     global int $gSelectEditPointsBit;
  77.     global int $gSelectCurveParmPointsBit;
  78.     global int $gSelectSurfaceParmPointsBit;
  79.     global int $gSelectMeshUVs;
  80.     global int $gSelectRotatePivots;
  81.     global int $gSelectScalePivots;
  82.     global int $gSelectJointPivots;
  83.     global int $gSelectSelectHandles;
  84.     global int $gSelectDimensions;
  85.     global int $gSelectUVLocators;
  86.     global int $gSelectSubdivMeshPoints;
  87.     global int $gSelectMeshEdge;
  88.  
  89.     string $polyEdges[] = `filterExpand -ex true -sm $gSelectMeshEdge`;
  90.     int $numEdges = size($polyEdges);
  91.     if (2 == $numEdges) {
  92.         getVerts();
  93.     } else if (0 != $numEdges) {
  94.         error("Select exactly two poly edges, each from different objects to align them");
  95.     }
  96.  
  97.     string $objectList[] = `filterExpand -ex true -sm $gSelectCVsBit -sm $gSelectMeshVerts -sm $gSelectLatticePoints -sm $gSelectParticles -sm $gSelectEditPointsBit -sm $gSelectSurfaceParmPointsBit -sm $gSelectCurveParmPointsBit -sm $gSelectMeshUVs -sm $gSelectRotatePivots -sm $gSelectScalePivots -sm $gSelectJointPivots -sm $gSelectSelectHandles -sm $gSelectDimensions -sm $gSelectUVLocators -sm $gSelectSubdivMeshPoints`;
  98.  
  99.     // make sure there are 4 objects selected
  100.     //
  101.     int $total = size($objectList);
  102.     if ( $total == 0 )
  103.     {
  104.         error("No points selected to snap.  Select four points: two " +
  105.             "from an object to move and two to indicate the destination.");
  106.         return;
  107.     }
  108.     else if ( $total < 4 )
  109.     {
  110.         error("Not enough points selected to snap.  Select four points: "+
  111.             "two from an object to move and two to indicate the destination.");
  112.         return;
  113.     }
  114.     else if ( $total > 4 )
  115.     {
  116.         warning("Too many points selected - using the last 4 only.");
  117.     }
  118.  
  119.     string $obj1 = $objectList[$total-4];
  120.     string $obj2 = $objectList[$total-3];
  121.     string $obj3 = $objectList[$total-2];
  122.     string $obj4 = $objectList[$total-1];
  123.  
  124.     // get the name of the object to move (with no component part). The first
  125.     // 2 points should be on the same object?
  126.     //
  127.     string $objectToMove = getObjectToMoveName( $obj1 );
  128.     string $tmpToMove = getObjectToMoveName( $obj2 );
  129.     string $tmp1 = getObjectToMoveName( $obj3 );
  130.     string $tmp2 = getObjectToMoveName( $obj4 );
  131.     if ( $objectToMove == $tmp1 || $objectToMove == $tmp2 )
  132.     {
  133.         error("Cannot snap to a point on the same object.");
  134.         return;
  135.     }
  136.     else
  137.     {
  138.         string $fullPath1[] = `ls -l $objectToMove`;
  139.         string $fullPath2[] = `ls -l $tmpToMove`;
  140.         if ( strcmp($fullPath1[0], $fullPath2[0]) != 0 )
  141.         {
  142.             error("The first two points must be on the same object.");
  143.             return;
  144.         }
  145.     }
  146.  
  147.     // If user wants to move a group, eg. he selects points on a
  148.     // face of a cube and wants the entire cube (group) to move
  149.     // $moveParent = 1: move the parent of the object
  150.     // $moveParent = 2: move the grandparent of the object
  151.     // $moveParent = 3: move the great-grandparent of the object
  152.     //
  153.     if( $moveParent > 0 ) {
  154.         string $parent[];
  155.         string $obj = $objectToMove;
  156.         for( $i = 0; $i < $moveParent; $i ++ ) {
  157.             $parent = `listRelatives -parent $obj`;
  158.             if( size($parent[0]) > 0 )  {
  159.                 $obj = $parent[0];
  160.             }
  161.         }
  162.         if( size($obj) > 0 )  {
  163.             $objectToMove = $obj;
  164.         }
  165.     }
  166.  
  167.     // get the world space position of each selected point object
  168.     //
  169.     float $pos1[] = `pointPosition $obj1`;
  170.     float $pos2[] = `pointPosition $obj2`;
  171.     float $pos3[] = `pointPosition $obj3`;
  172.     float $pos4[] = `pointPosition $obj4`;
  173.  
  174.     if ( $pos1[0] == $pos2[0] && $pos1[1] == $pos2[1] && $pos1[2] == $pos2[2] )
  175.     {
  176.         error("The first pair of points cannot be the same.");
  177.         return;
  178.     }
  179.     if ( $pos3[0] == $pos4[0] && $pos3[1] == $pos4[1] && $pos3[2] == $pos4[2] )
  180.     {
  181.         error("The second pair of points cannot be the same.");
  182.         return;
  183.     }
  184.  
  185.     // Arrange the points accordingly, based on the options
  186.     //
  187.  
  188.     float $tmpFloat;
  189.     if (3 == $option) {
  190.         // rightEdge snap..
  191.         // (Same as swapping pos3<=>pos4 and pos1<=>pos2)
  192.         //
  193.         $tmpFloat = $pos3[0]; $pos3[0] = $pos4[0]; $pos4[0] = $tmpFloat;
  194.         $tmpFloat = $pos3[1]; $pos3[1] = $pos4[1]; $pos4[1] = $tmpFloat;
  195.         $tmpFloat = $pos3[2]; $pos3[2] = $pos4[2]; $pos4[2] = $tmpFloat;
  196.  
  197.         $tmpFloat = $pos1[0]; $pos1[0] = $pos2[0]; $pos2[0] = $tmpFloat;
  198.         $tmpFloat = $pos1[1]; $pos1[1] = $pos2[1]; $pos2[1] = $tmpFloat;
  199.         $tmpFloat = $pos1[2]; $pos1[2] = $pos2[2]; $pos2[2] = $tmpFloat;
  200.  
  201.     } else if (2 == $option) {
  202.         // midEdge snap..
  203.         //
  204.         // (Same as using midpoints as the starting point of the vectors)
  205.         // Push pos1 to the midpoint, and push pos3 to the midpoint..
  206.         //
  207.  
  208.         $pos1[0] = 0.5 * ($pos1[0] + $pos2[0]);
  209.         $pos1[1] = 0.5 * ($pos1[1] + $pos2[1]);
  210.         $pos1[2] = 0.5 * ($pos1[2] + $pos2[2]);
  211.  
  212.         $pos3[0] = 0.5 * ($pos3[0] + $pos4[0]);
  213.         $pos3[1] = 0.5 * ($pos3[1] + $pos4[1]);
  214.         $pos3[2] = 0.5 * ($pos3[2] + $pos4[2]);
  215.     }
  216.     
  217.     // calculate the translation amount - the first point on each pair is the
  218.     // point to use for translation
  219.     //
  220.     float $distance[];
  221.     $distance[0] = $pos3[0] - $pos1[0];
  222.     $distance[1] = $pos3[1] - $pos1[1];
  223.     $distance[2] = $pos3[2] - $pos1[2];
  224.  
  225.      // move the first object by that amount
  226.     //
  227.     evalEcho("move -r " + $distance[0] + " " + $distance[1] + " " + $distance[2] + " " + $objectToMove);
  228.  
  229.     // define the two vectors for each pair of points
  230.     //
  231.     float $axis1[];
  232.     $axis1[0] = $pos2[0] - $pos1[0];
  233.     $axis1[1] = $pos2[1] - $pos1[1];
  234.     $axis1[2] = $pos2[2] - $pos1[2];
  235.     float $axis2[];
  236.     $axis2[0] = $pos4[0] - $pos3[0];
  237.     $axis2[1] = $pos4[1] - $pos3[1];
  238.     $axis2[2] = $pos4[2] - $pos3[2];
  239.  
  240.     // get the angle (in radians) between the two vectors and the axis of
  241.     // rotation
  242.     //
  243.     float $dotProduct = dotProduct( $axis1, $axis2, 1 );
  244.     $dotProduct = clamp(-1.0, 1.0, $dotProduct);
  245.     float $angle = acos($dotProduct);
  246.     float $crossProduct[] = crossProduct( $axis1, $axis2, 1, 1 );
  247.  
  248.      // rotate the first object about the pivot point (the pivot is defined 
  249.     // by the first point from the second pair of points  - i.e. point 3 from 
  250.     // the inputs above)
  251.     //
  252.     float $rotation[];
  253.     xyzRotation( $angle, $crossProduct, $rotation );
  254.     evalEcho("rotate -r -p " + $pos3[0] + " " + $pos3[1] + " " + $pos3[2] + " " + $rotation[0] + "rad " + $rotation[1] + "rad " + $rotation[2] + "rad " + $objectToMove);
  255. }
  256.